package com.td.server;

import java.util.Iterator;
import java.util.List;

import com.td.core.obj.Player;
import com.td.core.obj.TDGame;
import com.td.server.obj.TDBotFactory;

public class GameContainer {
	
	private TDGame game;
	private GameController gameConroller;
	
	private List<ClientContainer> players;
	
	public GameContainer(List<ClientContainer> players) {
		this.players = players;
		Iterator<ClientContainer> it = players.iterator();
		while(it.hasNext()) {
			it.next().setParent(this);
		}
		
		game = new TDGame(TDGame.MAX_PLAYERS);
		
		Player[] pl = game.getPlayers();
		for(int i = players.size(); i < TDGame.MAX_PLAYERS; i++) {
			pl[i] = TDBotFactory.getInstance().createTDBot();
			pl[i].getStats().setName("Bot " + i);
		}
		
		gameConroller = new GameController(game);
	}

	public GameController getGameConroller() {
		return gameConroller;
	}

	public TDGame getGame() {
		return game;
	}
	
	public void update() {
		gameConroller.update();
		Iterator<ClientContainer> it = players.iterator();
		while(it.hasNext()) {
			ClientContainer cc = it.next();
			cc.send(game.getCopy());
		}
		
	}
}
